package sqlmap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;
import sun.net.www.protocol.http.HttpURLConnection;

/**
 * Hello world!
 *
 */
public class ManualParser {

    public static void main(String[] args) {
        List<String> sourceCode = new ArrayList<String>();

        try {
            SocketAddress sa = new InetSocketAddress("127.0.0.1", 8080);
            Proxy p = new Proxy(Proxy.Type.HTTP, sa);
            HttpURLConnection hack = new HttpURLConnection(new URL("http", "sqlmap.sourceforge.net", "/doc/README.html"), p);
            hack.setRequestMethod("GET");
            hack.setRequestProperty("USER-AGENT", "sqlmap-manual-to-freemap");
            hack.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            hack.setRequestProperty("Accept-Language", "nb,no;q=0.8,nn;q=0.6,en-us;q=0.4,en;q=0.2");
            hack.setRequestProperty("Accept-Encoding", "plain");
            hack.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            hack.setRequestProperty("Keep-Alive", "30");
            //                hack.setRequestProperty("Proxy-Connection", "keep-alive");
            hack.connect();

            System.out.println("Connected.. retrieving input stream");
            BufferedReader stream = new BufferedReader(new InputStreamReader(hack.getInputStream()));
            System.out.println("Got the stream. Now lets read response");
            Tidy tidy = new Tidy();
            tidy.setQuiet(true);
            tidy.setShowWarnings(false);
            Document tidyDOM = tidy.parseDOM(hack.getInputStream(), null);

            NodeList elementsByTagName = tidyDOM.getElementsByTagName("*");
            for (int x = 0; x < elementsByTagName.getLength(); x++) {
                Node item = elementsByTagName.item(x);
//                if (item.getNodeName().toLowerCase().equals("h2")) {
//                        System.out.println(printChildren(item));
                        System.out.println(getNodeValues(item));
//                }
            }
            String htmlLine = "";
            while ((htmlLine = stream.readLine()) != null) {
                sourceCode.add(htmlLine);
            }
            stream.close();
        } catch (IOException ex) {
            Logger.getLogger(ManualParser.class.getName()).log(Level.SEVERE, null, ex);
        }

//        for (String line : sourceCode) {
//            if (line.contains("#toc5.")) {
//                System.out.println(line);
//            }
////            System.out.println(line);
//        }
    }
    
    private static String printChildren(Node n) {
        String result = "";
        if (n.hasChildNodes()) {
            for (int x = 0; x < n.getChildNodes().getLength(); x++) {
                    result += n.getChildNodes().item(x).getNodeName() + " - " 
                            + n.getChildNodes().item(x).getNodeValue() + " - " 
                            + n.getChildNodes().item(x).getTextContent() + " - " 
                            + n.getChildNodes().item(x).getNodeType() + "\r\n";
                    if (n.getChildNodes().item(x).hasChildNodes()) {
                        result += printChildren(n.getChildNodes().item(x));
                    }
            }
        }
        return result;
    }
    
    private static String getNodeValues(Node n) {
        String result = "";
        if (n.hasChildNodes()) {
            for (int x = 0; x < n.getChildNodes().getLength(); x++) {
                    result += n.getChildNodes().item(x).getNodeValue() + "\r\n";
                    if (n.getChildNodes().item(x).hasChildNodes()) {
                        result += getNodeValues(n.getChildNodes().item(x));
                    }
            }
        }
        return result;
    }    
}
